import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'package:test_flutter/entity/todo_item.dart';
import 'package:test_flutter/entity/user_item.dart';

///Hive 数据操作
class DBUtils {
  ///实例
  static DBUtils instance = DBUtils();

  late Box tagBox;
  late Box todoBox;
  late Box userBox;

  ///初始化，需要再main.dart调用
  static Future<void> init() async {
    ///初始化数据库地址
    var directory = await getApplicationDocumentsDirectory();
    print("document dir path:${directory.path}");
    Hive.init(directory.path);

    ///注册自定义对象（实体）
    Hive.registerAdapter(TodoItemAdapter());
    Hive.registerAdapter(UserInfoAdapter());

    instance.tagBox = await Hive.openBox("tags");
    instance.todoBox = await Hive.openBox('todo');
    instance.userBox = await Hive.openBox('user');
  }

  ///初始化Box
  static Future<DBUtils> getInstance() async {
    return instance;
  }
}
